package com.czy.project.exceptionHandler;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.czy.project.util.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/********************************************************************
 * @brief
 * @version 0.1
 * @date 2022年09月06日 22:05
 * @author changziyang
 ********************************************************************/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public DataResult error(Exception e){
        return DataResult.builder()
                .message("执行了全局统一异常处理类")
                .errorBuild();
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public DataResult ArithmeticException(ArithmeticException e){
        return DataResult.builder()
                .message("执行了全局统一ArithmeticException异常处理")
                .errorBuild();
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public DataResult MyException(MyException e){
        log.error(ExceptionUtil.stacktraceToString(e));
        return DataResult.builder()
                .code(e.getCode())
                .message(e.getMessage())
                .errorBuild();
    }
}
